/**
 * Created by eva on 2019/12/29.
 */
function Baby() {
    this.x;
    this.y;
    this.angle;
    this.babyTails = [];
    this.babyTailTimer = 0;
    this.babyTailIndex = 0;
    this.babyEyes = [];
    this.babyEyeTimer = 0;
    this.babyEyeIndex = 0;
    this.babyEyeInterval = 2000;

    this.babyFade = [];
    this.babyFadeTimer = 0;
    this.babyFadeIndex = 0;

}
Baby.prototype = {
    init : function () {
        this.x = canWidth*0.5 - 50;
        this.y = canHeight*0.5 + 50;
        this.angle = 0;
        for(var i = 0; i < 8; i++){
            this.babyTails[i] = new Image();
            this.babyTails[i].src = "./src/babyTail"+i+".png";
        }

        for(var i = 0; i < 2; i++){
            this.babyEyes[i] = new Image();
            this.babyEyes[i].src = "./src/babyEye"+i+".png";
        }

        for(var i = 0; i < 20; i++){
            this.babyFade[i] = new Image();
            this.babyFade[i].src = "./src/babyFade"+i+".png";
        }
    },
    draw : function () {


        //lerp x,y
        this.x = lerpDistance(mom.x,this.x,0.98);
        this.y = lerpDistance(mom.y,this.y,0.98);

        //delta angle
        //math atan2(y,x);
        var deltaY = mom.y - this.y;
        var deltaX = mom.x - this.x;
        var beta = Math.atan2(deltaY,deltaX)+Math.PI;

        //
        this.babyTailTimer += deltaTime;
        if(this.babyTailTimer > 50){
            this.babyTailIndex++;
            this.babyTailIndex = this.babyTailIndex%8;
            this.babyTailTimer = 0;

        }

        this.babyEyeTimer += deltaTime;
        if(this.babyEyeTimer  > this.babyEyeInterval){
            this.babyEyeIndex = (this.babyEyeIndex+1)%2;
            this.babyEyeTimer %= this.babyEyeInterval;
            if(this.babyEyeIndex == 0){
                this.babyEyeInterval = Math.random()*1500 + 2000;
            }else{
                this.babyEyeInterval = 200;
            }
        }

        this.babyFadeTimer += deltaTime;
        if(this.babyFadeTimer > 300){
            this.babyFadeIndex++;
            this.babyFadeTimer %= 300;
            if(this.babyFadeIndex > 19){
                this.babyFadeIndex = 19;
                //game over
                gameOver = true;
            }
        }

        //lerp angle
        this.angle  = lerpAngle(beta, this.angle, 0.6);
        //ctx1
        ctx1.save();
        ctx1.translate(this.x,this.y);
        ctx1.rotate(this.angle);
        var tail = this.babyTails[this.babyTailIndex];
        var eye = this.babyEyes[this.babyEyeIndex];
        var body = this.babyFade[this.babyFadeIndex];
        ctx1.drawImage(tail, -tail.width*0.5+23, -tail.height*0.5);

        ctx1.drawImage(body, -body.width*0.5,-body.height*0.5);
        ctx1.drawImage(eye, -eye.width*0.5, -eye.height*0.5);
        ctx1.restore();
    }
}
